﻿using Microsoft.IdentityModel.Tokens;
using ServiceCore.Entity.Core;
using ServiceCore.Entity.Model;
using ServiceCore.Entity.Param.User;
using ServiceCore.Utility.Extension;

namespace ServiceCore.Service
{
    public partial class UserInfoService
    {
        public BaseParam<UserInfo, long> User_Save_Common_Verify(BaseParam<UserInfo, long> arg)
        {
            if (arg == null)
                return new BaseParam<UserInfo, long>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<UserInfo, long>("参数不能为空");
            if (string.IsNullOrEmpty(arg.Param.Account))
                return new BaseParam<UserInfo, long>("账号不能为空");            

            return arg;
        }

        public BaseParam<UserInfo, long> User_Add_Verify(BaseParam<UserInfo, long> arg)
        {
            arg = User_Save_Common_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;
            if (string.IsNullOrEmpty(arg.Param.Pwd))
                return new BaseParam<UserInfo, long>("密码不能为空");
            if (string.IsNullOrEmpty(arg.Param.UserName))
                return new BaseParam<UserInfo, long>("姓名不能为空");

            var argUser = new BaseParam<UserInfo, UserInfo>();
            argUser.Param = arg.Param;
            User_Exists(argUser, true);
            if (argUser.Data != null)
                return new BaseParam<UserInfo, long>("账号已存在");

            arg.Param.IsAdmin = 0;
            arg.Param.IsUsed = 1;
            arg.Param.CreateTime = DateTime.Now;
            arg.Param.Pwd = arg.Param.Pwd.EncodeDES();
            return arg;
        }

        public BaseParam<UserInfo, long> User_Edit_Verify(BaseParam<UserInfo, long> arg)
        {
            arg = User_Save_Common_Verify(arg);
            if (string.IsNullOrEmpty(arg.ErrorMsg) == false)
                return arg;
            if (arg.Param.Id == 0)
                return new BaseParam<UserInfo, long>("id不能为空");

            var argUser = new BaseParam<UserInfo, UserInfo>();
            argUser.Param = arg.Param;
            User_Exists(argUser, false);
            if (argUser.Data != null)
                return new BaseParam<UserInfo, long>("账号已存在");

            var user = this.GetById(arg.Param.Id);
            if(user == null)
                return new BaseParam<UserInfo, long>("信息不存在");
            if (string.IsNullOrEmpty(arg.Param.Pwd) == false)
                arg.Param.Pwd = arg.Param.Pwd.EncodeDES();
            else
                arg.Param.Pwd = user.Pwd;

            return arg;
        }

        public BaseParam<UserInfo, long> User_IsUsed_Verify(BaseParam<UserInfo, long> arg)
        {
            if (arg == null)
                return new BaseParam<UserInfo, long>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<UserInfo, long>("参数不能为空");
            if (arg.Param.Id == 0)
                return new BaseParam<UserInfo, long>("id不能为空");
            if (arg.Param.IsUsed == null)
                return new BaseParam<UserInfo, long>("是否启用不能为空");

            return arg;
        }

        public BaseParam<UserChangePwdParam, long> User_ChangePwd_Verify(BaseParam<UserChangePwdParam, long> arg)
        {
            if (arg == null)
                return new BaseParam<UserChangePwdParam, long>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<UserChangePwdParam, long>("参数不能为空");
            if (string.IsNullOrEmpty(arg.Param.Account))
                return new BaseParam<UserChangePwdParam, long>("账号不能为空");
            if (string.IsNullOrEmpty(arg.Param.OldPwd))
                return new BaseParam<UserChangePwdParam, long>("旧密码不能为空");
            if (string.IsNullOrEmpty(arg.Param.NewPwd))
                return new BaseParam<UserChangePwdParam, long>("新密码不能为空");

            var argUser = new BaseParam<UserInfo, UserInfo>();
            argUser.Param = new UserInfo() { Account = arg.Param.Account };
            User_Exists(argUser);
            if (argUser.Data == null)
                return new BaseParam<UserChangePwdParam, long>("用户不存在");

            if (argUser.Data.Pwd.EncodeDES() != arg.Param.OldPwd)
                return new BaseParam<UserChangePwdParam, long>("密码错误");

            return arg;
        }

        public BaseParam<UserInfo, long> User_Del_Verify(BaseParam<UserInfo, long> arg)
        {
            if (arg == null)
                return new BaseParam<UserInfo, long>("参数不能为空");
            if (arg.Id == 0)
                return new BaseParam<UserInfo, long>("id不能为空");

            return arg;
        }

        public BaseParam<UserInfo, UserInfo> User_Login_Verify(BaseParam<UserInfo, UserInfo> arg)
        {
            if (arg == null)
                return new BaseParam<UserInfo, UserInfo>("参数不能为空");
            if (arg.Param == null)
                return new BaseParam<UserInfo, UserInfo>("参数不能为空");
            if (string.IsNullOrEmpty(arg.Param.Account))
                return new BaseParam<UserInfo, UserInfo>("账号不能为空");
            if (string.IsNullOrEmpty(arg.Param.Pwd))
                return new BaseParam<UserInfo, UserInfo>("密码不能为空");

            return arg;
        }

        public BaseParam<UserInfo, long> User_LoginOut_Verify(BaseParam<UserInfo, long> arg)
        {
            if (arg == null)
                return new BaseParam<UserInfo, long>("参数不能为空");
            if (arg.Id == 0)
                return new BaseParam<UserInfo, long>("id不能为空");

            return arg;
        }
    }

}   
